package com.hkyl.hospital.web.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Check;
import com.hkyl.hospital.entity.Dept;
import com.hkyl.hospital.service.CheckService;
import com.hkyl.hospital.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author whd
 * @date 2023/07/10 15:33:32
 */
@RestController
@RequestMapping("check")
public class CheckController {

    @Autowired
    CheckService checkService;

    @Autowired
    DeptService deptService;

    Logger logger = LoggerFactory.getLogger(CheckController.class);
    @PostMapping("findAll")
    public ResultVo findAll(@RequestBody Map map) {
        PageInfo<Check> pageInfo = checkService.findAllByCondition(map);
        List<Dept> depts = deptService.findAllByMedical();
        Map<String, Object> resp = new HashMap<>();
        resp.put("pageInfo", pageInfo);
        resp.put("depts", depts);
        return ResultVo.success(resp);
    }

    @PostMapping("save")
    public ResultVo save(@RequestBody Check check) {
        int rows = checkService.add(check);
        return rows > 0 ? ResultVo.success("添加成功", check) : ResultVo.failure("添加失败");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Check check) {
        int rows = checkService.update(check);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }

    @PostMapping("delete")
    public ResultVo delete(@RequestBody String checkId) {
        if (StringUtils.hasText(checkId)) {
            throw new RuntimeException("删除id为空");
        }
        int rows = checkService.delete(Integer.valueOf(checkId));
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

/*    @PostMapping("condition")
    public ResultVo findByCondition(@RequestBody Map<String, Object> condition,HttpSession session) {
        condition.put("pageNum",session.getAttribute("pageNum"));
        condition.put("pageSize",session.getAttribute("pageSize"));
        PageInfo<Check> pageInfo = checkService.findAllByCondition(condition);
        return ResultVo.success(pageInfo);
    }*/

    @PostMapping("list")
    public ResultVo list(){
        return ResultVo.success(checkService.listCheck());
    }
}
